package cn.lgy.basis.controller;

import cn.lgy.basis.entity.User;
import cn.lgy.basis.service.UserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2021-06-02 16:16:01
 */
@Slf4j
@RestController
@RequestMapping("user")
@Api(value = "用户信息管理")
public class UserController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    @ApiOperation(value = "查询单个用户", notes = "通过用户ID获取用户信息")
    public User selectOne(Integer id) {
        return this.userService.queryById(id);
    }

    @GetMapping("selectPage")
    @ApiOperation(value = "分页查询", notes = "分页查询用户信息，根据用户名密码查询")
    public List<User> selectPage(Integer pageNo, Integer pageSize, String username, String password) {
        startPage();
        User user = new User();
        user.setUserName(username);
        user.setPassword(password);
        List<User> list = userService.queryAll(user);
        long total = new PageInfo<>(list).getTotal();
        log.info("user total: {}", total);
        return list;
    }

    @PostMapping("selectPage2")
    @ApiOperation(value = "分页查询2", notes = "分页查询用户信息，根据用户名密码查询")
    public List<User> selectPage2(@Valid @RequestBody User user, Integer pageNo, Integer pageSize) {
        startPage();
        List<User> list = userService.queryAll(user);
        long total = new PageInfo<>(list).getTotal();
        log.info("user total: {}", total);
        return list;
    }

    @GetMapping("getUserByName")
    @ApiOperation("根据用户名获取用户信息--测试事务")
    public User getUserByName(String username) {
        return userService.getUserByName(username);
    }
}